package com.adam.example.search;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.json.JsonData;
import com.adam.example.ElasticsearchClientHolder;
import com.adam.example.types.Branch;
import com.adam.example.types.Employee;

import java.io.IOException;
import java.util.List;

public class SearchCompanyExample {

    public static void main(String[] args) throws IOException {
        ElasticsearchClient client = ElasticsearchClientHolder.getINSTANCE().initializeAndGetClient();

        SearchRequest searchRequest = SearchRequest.of(builder -> builder
                        .index("test_company")
                        .query(queryBuilder -> queryBuilder
                                .hasChild(hasChildQueryBuilder -> hasChildQueryBuilder
                                        .type("employee")
                                        .query(queryBuilder1 -> queryBuilder1
                                                .range(rangeQueryBuilder -> rangeQueryBuilder
                                                        .field("dob").gte(JsonData.of("1980-01-01"))
                                                )
                                        )
                                )
                )
        );
        SearchResponse<Branch> searchResponse = client.search(searchRequest, Branch.class);
        HitsMetadata<Branch> hitsMetadata = searchResponse.hits();
        System.out.println("搜索到" + hitsMetadata.total().value() + "条数据");
        List<Hit<Branch>> branchHitList = hitsMetadata.hits();
        for(int i=0;i<branchHitList.size();i++) {
            Hit<Branch> branchHit = branchHitList.get(i);
            Branch branch = branchHit.source();
            System.out.println("文档[" + (i+1) + "][id=" + branchHit.id() + "][评分" + branchHit.score() + "]:" + branch);
        }

        SearchRequest searchRequest1 = SearchRequest.of(builder -> builder
                .index("test_company")
                .query(queryBuilder -> queryBuilder
                        .hasParent(hasParentQueryBuilder -> hasParentQueryBuilder
                                .parentType("branch")
                                .query(queryBuilder1 -> queryBuilder1
                                        .match(matchQueryBuilder -> matchQueryBuilder
                                                .field("country").query("UK")
                                        )
                                )
                        )
                )
        );
        SearchResponse<Employee> searchResponse1 = client.search(searchRequest1, Employee.class);
        HitsMetadata<Employee> hitsMetadata1 = searchResponse1.hits();
        System.out.println("搜索到" + hitsMetadata1.total().value() + "条数据");
        List<Hit<Employee>> employeeHitList = hitsMetadata1.hits();
        for(int i=0;i<employeeHitList.size();i++) {
            Hit<Employee> employeeHit = employeeHitList.get(i);
            Employee employee = employeeHit.source();
            System.out.println("文档[" + (i+1) + "][id=" + employeeHit.id() + "][评分" + employeeHit.score() + "]:" + employee);
        }

        ElasticsearchClientHolder.getINSTANCE().close();
    }

}
